﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 圆心计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Location A = new Location(double.Parse(textBoxA_X.Text), double.Parse(textBoxA_Y.Text));
                Location B = new Location(double.Parse(textBoxB_X.Text), double.Parse(textBoxB_Y.Text));
                Location C = new Location(double.Parse(textBoxC_X.Text), double.Parse(textBoxC_Y.Text));
                Vector AB = new Vector(A,B);
                Vector AC = new Vector(A,C);
                Vector BC = new Vector(B,C);
                Judge(AB,AC);
                double cosA = Multiplicate(AB,AC) / (AB.Length * AC.Length);
                double a = Math.Acos(cosA);
                double sTriangle = AB.Length * AC.Length * Math.Sin(a)/2;
                double rInner = (2 * sTriangle) / (AB.Length + AC.Length + BC.Length);
                double rExternal = BC.Length / (2 * Math.Sin(a));

                //x = (a * x1 + b * x2 + c * x3) / (a + b + c) 
                //y = (a * y1 + b * y2 + c * y3) / (a + b + c)
                double innerX = (BC.Length * A.X + AC.Length * B.X + AB.Length * C.X) / (AB.Length + AC.Length + BC.Length);
                double innerY = (BC.Length * A.Y + AC.Length * B.Y + AB.Length * C.Y) / (AB.Length + AC.Length + BC.Length);
                double externalX = 0,externalY = 0;
                GetExternalLocations(A.X, B.X, C.X, A.Y, B.Y, C.Y,ref externalX,ref externalY);

                lblExternalLocation.Text = ("("+ Math.Round(externalX,2) +","+ Math.Round(externalY, 2) + ")");
                lblInnerLocation.Text = ("("+ Math.Round(innerX,2) +","+ Math.Round(innerY, 2) + ")");
                lblExternalRadius.Text = (Math.Round(rExternal,2)+"");
                lblInnerRadius.Text = (Math.Round(rInner,2)+"");

            }
            catch (NoTrianglesException) {
                MessageBox.Show("无法构成三角形，请重新输入坐标！");
            }
            catch(Exception)
            {
                MessageBox.Show("输入格式错误，请重新输入坐标！");
            }
        }

        private void Judge(Vector a, Vector b)
        {
            if(a.X * b.Y == a.Y * b.X) {
                throw new NoTrianglesException();
            }
        }
        private double Multiplicate(Vector a,Vector b)
        {
            return a.X * b.X + a.Y * b.Y;
        }

        private void GetExternalLocations(double x1,double x2,double x3,double y1,double y2,double y3,
            ref double x0,ref double y0)
        {
            x0 = ((y2 - y1) * (y3 * y3 - y1 * y1 + x3 * x3 - x1 * x1) - (y3 - y1) * (
            y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1)) /
            (2 * (x3 - x1) * (y2 - y1) - 2 * (x2 - x1) * (y3 - y1));


            y0 = ((x2 - x1) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1) - (x3 - x1) * (
            x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1)) /
            (2 * (y3 - y1) * (x2 - x1) - 2 * (y2 - y1) * (x3 - x1));
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
